package com.hcy.rabbitmanagerapi.controller;

import com.hcy.rabbitbasicmodel.dto.MyPage;
import com.hcy.rabbitbasicmodel.enums.Clients;
import com.hcy.rabbitbasicmodel.enums.Log_Code;
import com.hcy.rabbitbasicmodel.model.ResponseData;
import com.hcy.rabbitbasicmodel.model.ReturnResult;
import com.hcy.rabbitbasicmodel.signl.Add;
import com.hcy.rabbitbasicmodel.signl.Delete;
import com.hcy.rabbitbasicmodel.signl.Update;
import com.hcy.rabbitmanagerapi.utils.CheckTokenAccess;
import com.hcy.rabbitmanagerapi.utils.MyAutoOptLog;
import com.hcy.userclient.client.UserAuthClient;
import com.hcy.usermodel.dto.request.MenuRequestDTO;
import com.hcy.usermodel.dto.request.RoleUpdateRequestDTO;
import com.hcy.usermodel.dto.request.UploadRoutesRequestDTO;
import com.hcy.usermodel.dto.request.UserAuthRequestDTO;
import com.hcy.usermodel.dto.response.MenuResponseDTO;
import com.hcy.usermodel.dto.response.RoleResponseDTO;
import com.hcy.usermodel.dto.response.RoutesResponseDTO;
import com.hcy.usermodel.dto.response.UserAuthsResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author huchenying
 * @Description 权限相关接口
 * @Date 2022/4/21 20:30
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class ManagerUserController {

    @Autowired
    CheckTokenAccess checkTokenAccess;
    @Autowired
    UserAuthClient userAuthClient;
    @Autowired
    PasswordEncoder passwordEncoder;

    /**
     * @Author huchenying
     * @Description 上传路由表
     * @Date 2022/4/21 20:31
     **/
    @PostMapping("/uploadRoutes")
    @MyAutoOptLog(Log_Code.U_ROUTES)
    public ReturnResult uploadRoutes(@Validated @RequestBody UploadRoutesRequestDTO requestDTO) {
        log.info("uploadRoutes start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<Object> responseData = userAuthClient.uploadRoutes(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("获取失败");
            }
            return ReturnResult.success();
        } catch (Exception e) {
            log.error("e ->  {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("系统繁忙");
        }
    }

    /**
     * @Author huchenying
     * @Description 获取路由表
     * @Date 2022/4/21 20:31
     **/
    @PostMapping("/getRoutes")
    public ReturnResult getRoutes() {
        log.info("uploadRoutes start");
        try {
            ResponseData<List<RoutesResponseDTO>> routesList = userAuthClient.getRoutesList();
            if (routesList.getCode().noSuccess()) {
                return ReturnResult.fail("获取失败");
            }
            return ReturnResult.success().setData(routesList.getData());
        } catch (Exception e) {
            log.error("e ->  {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("系统繁忙");
        }
    }

    /**
     * @Author huchenying
     * @Description 获取用户
     * @Date 2022/4/21 20:31
     **/
    @PostMapping("/getUserList")
    public ReturnResult getUserList(@RequestBody UserAuthRequestDTO requestDTO) {
        log.info("getUserList start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            //先不分页
            requestDTO.setBusinessCode(Clients.MANAGER.getName());
            ResponseData<MyPage<UserAuthsResponseDTO>> userList = userAuthClient.getUserList(requestDTO);
            if (userList.getCode().noSuccess()) {
                return ReturnResult.fail("获取失败");
            }
            return ReturnResult.success().setData(userList.getData());
        } catch (Exception e) {
            log.error("e ->  {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("系统繁忙");
        }
    }

    /**
     * @Author huchenying
     * @Description 添加用户
     * @Date 2022/4/21 20:31
     **/
    @PostMapping("/addUser")
    @MyAutoOptLog(Log_Code.A_USER)
    public ReturnResult addUser(@Validated(Add.class) @RequestBody UserAuthRequestDTO requestDTO) {
        log.info("addUser start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            String encode = passwordEncoder.encode(requestDTO.getPassword());
            requestDTO.setPassword(encode);
            requestDTO.setBusinessCode(Clients.MANAGER.getName());
            ResponseData<UserAuthsResponseDTO> responseData = userAuthClient.insertOrUpdateUser(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("获取失败");
            }
            return ReturnResult.success().setData(responseData.getData());
        } catch (Exception e) {
            log.error("e ->  {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("系统繁忙");
        }
    }

    /**
     * @Author huchenying
     * @Description 删除用户
     * @Date 2022/4/21 20:31
     **/
    @PostMapping("/deleteUser")
    @MyAutoOptLog(Log_Code.D_USER)
    public ReturnResult deleteUser(@Validated(Delete.class) @RequestBody UserAuthRequestDTO requestDTO) {
        log.info("deleteUser start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        if(requestDTO.getUsername().equals("admin")){
            return ReturnResult.fail("该用户不能删除");
        }
        try {
            requestDTO.setBusinessCode(Clients.MANAGER.getName());
            ResponseData<Object> responseData = userAuthClient.deleteUser(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("删除失败");
            }
            return ReturnResult.success();
        } catch (Exception e) {
            log.error("e ->  {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("系统繁忙");
        }
    }

    /**
     * @Author huchenying
     * @Description 获取角色
     * @Date 2022/4/21 20:31
     **/
    @PostMapping("/getRoleList")
    public ReturnResult getRoleList() {
        log.info("getRole start ...");
        try {
            UserAuthRequestDTO userAuthRequestDTO = new UserAuthRequestDTO();
            userAuthRequestDTO.setBusinessCode(Clients.MANAGER.getName());
            ResponseData<List<RoleResponseDTO>> roleList = userAuthClient.getRoleList(userAuthRequestDTO);
            if (roleList.getCode().noSuccess()) {
                return ReturnResult.fail("获取失败");
            }
            return ReturnResult.success().setData(roleList.getData());
        } catch (Exception e) {
            log.error("e ->  {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("系统繁忙");
        }
    }

    /**
     * @Author huchenying
     * @Description 添加角色
     * @Date 2022/4/21 20:31
     **/
    @PostMapping("/addRole")
    @MyAutoOptLog(Log_Code.A_ROLE)
    public ReturnResult addRole(@Validated(Add.class) @RequestBody RoleResponseDTO requestDTO) {
        log.info("addRole start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            requestDTO.setBusinessCode(Clients.MANAGER.getName());
            ResponseData<RoleResponseDTO> responseData = userAuthClient.addRole(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("添加角色失败");
            }
            return ReturnResult.success().setData(responseData.getData());
        } catch (Exception e) {
            log.error("e ->  {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("系统繁忙");
        }
    }

    /**
     * @Author huchenying
     * @Description 删除角色
     * @Date 2022/4/21 20:31
     **/
    @PostMapping("/deleteRole")
    @MyAutoOptLog(Log_Code.A_ROLE)
    public ReturnResult deleteRole(@Validated(Delete.class) @RequestBody RoleResponseDTO requestDTO) {
        log.info("deleteRole start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            requestDTO.setBusinessCode(Clients.MANAGER.getName());
            ResponseData<Object> responseData = userAuthClient.deleteRole(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("删除失败");
            }
            return ReturnResult.success();
        } catch (Exception e) {
            log.error("e ->  {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("系统繁忙");
        }
    }

    /**
     * @Author huchenying
     * @Description 绑定角色
     * @Date 2022/4/21 20:31
     **/
    @PostMapping("/updateUserRole")
    @MyAutoOptLog(Log_Code.U_PERMISSION)
    public ReturnResult updateUserRole(@Validated @RequestBody RoleUpdateRequestDTO requestDTO) {
        log.info("updateUserRole start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            if(requestDTO.getIsChangePassword()){
                String encode = passwordEncoder.encode(requestDTO.getPassword());
                requestDTO.setPassword(encode);
            }
            ResponseData<Object> responseData = userAuthClient.updateUserRole(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("更新失败");
            }
            return ReturnResult.success();
        } catch (Exception e) {
            log.error("e ->  {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.fail("系统繁忙");
        }
    }

    /**
     * @Author huchenying
     * @Description 获取菜单
     * @Date 2022/4/23 18:25
     **/
    @PostMapping("/getMenuList")
    public ReturnResult getMenuList() {
        log.info("getMenuList start ");
        try {
            ResponseData<MenuResponseDTO> menuList = userAuthClient.getMenuList();
            if (menuList.getCode().noSuccess()) {
                return ReturnResult.fail("获取菜单失败");
            }
            return ReturnResult.success().setData(menuList.getData());
        } catch (Exception e) {
            log.info("e -> {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.exception("获取菜单异常");
        }
    }

    /**
     * @Author huchenying
     * @Description 添加菜单
     * @Date 2022/4/23 18:25
     **/
    @PostMapping("/addMenu")
    @MyAutoOptLog(Log_Code.A_MENU)
    public ReturnResult addMenu(@Validated(Add.class) @RequestBody MenuRequestDTO requestDTO) {
        log.info("addMenu start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<MenuResponseDTO> responseData = userAuthClient.addMenu(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("添加菜单失败");
            }
            return ReturnResult.success().setData(responseData.getData());
        } catch (Exception e) {
            log.info("e -> {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.exception("系统繁忙");
        }
    }

    /**
     * @Author huchenying
     * @Description 删除菜单
     * @Date 2022/4/23 18:25
     **/
    @PostMapping("/deleteMenu")
    @MyAutoOptLog(Log_Code.D_MENU)
    public ReturnResult deleteMenu(@Validated(Delete.class) @RequestBody MenuRequestDTO requestDTO) {
        log.info("deleteMenu start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            ResponseData<Object> responseData = userAuthClient.deleteMenu(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("删除菜单失败");
            }
            return ReturnResult.success();
        } catch (Exception e) {
            log.info("e -> {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.exception("系统繁忙");
        }
    }

    /**
     * @Author huchenying
     * @Description 给角色更新菜单绑定关系
     * @Date 2022/4/23 18:25
     **/
    @PostMapping("/updateRoleMenu")
    @MyAutoOptLog(Log_Code.U_MENU)
    public ReturnResult updateRoleMenu(@Validated(Update.class) @RequestBody MenuRequestDTO requestDTO) {
        log.info("updateRoleMenu start param -> {}", ToStringBuilder.reflectionToString(requestDTO));
        try {
            requestDTO.setBusinessCode(Clients.MANAGER.getName());
            ResponseData<Object> responseData = userAuthClient.updateRoleMenu(requestDTO);
            if (responseData.getCode().noSuccess()) {
                return ReturnResult.fail("删除菜单失败");
            }
            return ReturnResult.success();
        } catch (Exception e) {
            log.info("e -> {}", ToStringBuilder.reflectionToString(e));
            return ReturnResult.exception("系统繁忙");
        }
    }
}
